package com.iscas.loadstorm.isloadstorm.controller;

import com.iscas.loadstorm.isloadstorm.common.CustomResult;
import com.iscas.loadstorm.isloadstorm.service.LoadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;


@RestController
@RequestMapping("/load")
public class LoadController {
    @Autowired
    private LoadService loadService;

    @PostMapping("/run/{loadId}")
    public CustomResult runLoadTest(@PathVariable String loadId) {
        try {
            String testId = loadService.runLoadTest(loadId);
            return CustomResult.ok(testId);
        } catch (Exception e) {
            return CustomResult.fail(e.getMessage());
        }
    }

    @GetMapping("/status/{testId}")
    public CustomResult getTestStatus(@PathVariable String testId) {
        try {
            Map<String, Object> status = loadService.getTestStatus(testId);
            return CustomResult.ok(status);
        } catch (Exception e) {
            return CustomResult.fail(e.getMessage());
        }
    }

    @PostMapping("/stop/{testId}")
    public CustomResult stopTest(@PathVariable String testId) {
        try {
            loadService.stopTest(testId);
            return CustomResult.ok("Test stopped successfully");
        } catch (Exception e) {
            return CustomResult.fail(e.getMessage());
        }
    }
}